from typing import Optional

from fastapi import APIRouter, Depends, Query, Body, Request
from sqlalchemy.orm import Session

from server.app.cloud_ide.model import Ide
from server.app.cloud_ide.schema.ide.request.ide_create_request import IdeCreateRequest
from server.app.cloud_ide.schema.ide.request.ide_update_request import IdeUpdateRequest
from server.app.cloud_ide.service.ide_service import IdeService
from server.framework.core.http_schemas import CommonResponseSchema, TableResponseSchema
from server.framework.core.injects import get_db
from server.framework.core.settings import settings

ide_api = APIRouter()
ide_service = IdeService()


@ide_api.get("/get_entity", name='获取ide实体')
async def get_entity(id: str = Query(None, description='开发环境id'),
                     db: Session = Depends(get_db)) -> Ide:
    entity = ide_service.get_ide_by_id(db, id)
    return entity


@ide_api.get("/list", response_model=TableResponseSchema, name='获取IDE列表')
async def list(
        request: Request,
        name: Optional[str] = Query(None, description='过滤条件:ide名称'),
        current: Optional[int] = Query(1, description='当前页数'),
        page_size: Optional[int] = Query(10, description='每页记录的数量', alias='pageSize'),
        db: Session = Depends(get_db)) -> TableResponseSchema:
    """
    获取IDE列表
    :return:
    """
    items, total = ide_service.get_ide_list(db, request.state.user_id, name, page_size, current)
    return TableResponseSchema(data=items, total=total, page=page_size, success=True)


@ide_api.post("/create", response_model=CommonResponseSchema, name='创建IDE')
async def create(request: Request,
                 data: IdeCreateRequest = Body(None, description='IDE创建实体'),
                 db: Session = Depends(get_db)) -> CommonResponseSchema:
    """
    创建IDE
    :return:
    """
    rs = ide_service.create_ide(db, request.state.user_name, request.state.user_id,
                                data)
    if rs:
        return CommonResponseSchema(data={}, message=f'创建成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'创建失败', success=False)


@ide_api.get("/delete", response_model=CommonResponseSchema, name='删除IDE')
async def delete(ids: str = Query(None, description='镜像id，多条记录以逗号分割'),
                 db: Session = Depends(get_db)) -> CommonResponseSchema:
    rs = ide_service.delete_ide(db, ids.split(','))
    if rs:
        return CommonResponseSchema(data={}, message=f'删除成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'删除失败', success=False)


@ide_api.post("/edit", response_model=CommonResponseSchema, name='编辑IDE')
async def edit(data: IdeUpdateRequest = Body(None, description='编辑IDE实体'),
               db: Session = Depends(get_db)) -> CommonResponseSchema:
    rs = ide_service.update_ide(db, data)
    if rs:
        return CommonResponseSchema(data={}, message=f'编辑成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'编辑失败', success=False)


@ide_api.get("/get_ide_url", name='获取IDE地址', response_model=CommonResponseSchema)
async def get_ide_url(id: str = Query(None, description='开发环境id'),
                      db: Session = Depends(get_db)):
    ide = ide_service.get_ide_by_id(db, id)
    return CommonResponseSchema(data={'url': f'http://{ide.id}.{settings.ide_domain}'}, message=f'')


@ide_api.get("/start", name='启动IDE', response_model=CommonResponseSchema)
async def start(id: str = Query(None, description='开发环境id'),
                db: Session = Depends(get_db)):
    """
    启动IDE
    :return:
    """

    rs = ide_service.start_ide(db, id)
    if rs:
        return CommonResponseSchema(data={}, message=f'启动成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'启动失败', success=False)


@ide_api.get("/stop", name='暂停IDE', response_model=CommonResponseSchema)
async def stop(id: str = Query(None, description='开发环境id'),
               db: Session = Depends(get_db)):
    """
    停止IDE
    :return:
    """
    rs = ide_service.stop_ide(db, id)
    if rs:
        return CommonResponseSchema(data={}, message=f'停止成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'停止失败', success=False)
